<?php
/**
 *  ### Using the alias `i0' for the tree index table
 *
 *
 */
namespace Dclub\modules\Catalog;

class Action_SelectTreeNodeByPath
    extends \Yz\modules\Generic\Action_Select
{

    /**
     *  @var array
     */
    protected $path;


    /************************************
     *         Public methods          *
     ************************************/


    /**
     *  @param array-type $path
     *  @return this
     */
    public function setPath( $path )
    {
        debug();
        $path = \Yz_Array::valueOf($path);
        if (null === $path) {
            throw new \Yz_Exception_InvalidArgument("Path must be an array");
        }
        if (! $path) {
            throw new \Yz_Exception_InvalidArgument("Path must not be empty");
        }
        $this->path = $path;
        return $this;
    }



    /** fetch path
     *  @return Yz_Hash
     */
    public function fetch()
    {
        return $this->getStatement()->fetchAll();
    }

    /************************************
     *       Protected methods          *
     ************************************/


    /**
     *  Modified SELECT statement creator
     *
     */
    protected function buildSelect()
    {
        debug();
        $path = $this->path;
        if (null === $path) {
            throw new \Yz_Exception("Path not set");
        }
        $sql = parent::buildSelect();
        $indexTable = $this->tableList[1];
        $sql
            ->where("i0.id={$from}")
            ->orderBy("i0.depth");
        return $sql;
    }



}
